package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.HotArtistAllAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.entity.ArtistListEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.view.ReplaceFragment;

import java.util.List;

/**
 * Created by dllo on 15/10/26.
 */
public class HotArtistAllFragment extends BaseFragment {
    private TextView titleText;
    private LinearLayout back;
    private GridView gridView;
    private StringRequest request;
    private VolleySingleton requestQueue;
    private Context context;
    private List<ArtistListEntity.ArtistEntity> artistList;
    private HotArtistAllAdapter adapter;
    private View v;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v= inflater.inflate(R.layout.activity_hot_artist_all,null);
        init();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new HotArtistAllAdapter(context);
        requestQueue = VolleySingleton.getInstance(context);

    titleText.setText(R.string.ARTIST_HOT);
        String url = Url.HOT_ARTIST_ALL;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ArtistListEntity artistListEntity = gson.fromJson(response,ArtistListEntity.class);
                if (artistListEntity != null) {
                    artistList = artistListEntity.getArtist();
                    adapter.setData(artistList);
                    gridView.setAdapter(adapter);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context,getString(R.string.NET),Toast.LENGTH_LONG).show();
            }
        });

        requestQueue.addQueue(request);


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                String artistId = artistList.get(position).getTing_uid();
                bundle.putString(getString(R.string.artistID), artistId);
                ArtistDetailFragment fragment = new ArtistDetailFragment();
                fragment.setArguments(bundle);
                new ReplaceFragment(fragment,getFragmentManager(),R.id.fl_main,true,null);

            }
        });

    }
    private void init() {
        context = getActivity();
        back = (LinearLayout) v.findViewById(R.id.back_btn);

        gridView = (GridView) v.findViewById(R.id.hot_artist_all_gridView);

        titleText = (TextView) v.findViewById(R.id.set_back_text);
    }
}
